const local_key = 'todomvc'
/**
 * 获取任务列表
 */
export function fetch() {
    const result = localStorage.getItem(local_key)
    if (result) {
        return JSON.parse(result)
    }
    return []
}

/**
 * 保存所有任务
 * @param {Array} todos 
 */
export function save(todos) {
    localStorage.setItem(local_key, JSON.stringify(todos))
}

/**
 * 生成唯一编号
 */
export function generateId() {
    return +new Date() + Math.random().toString(16).substr(2, 4)
}

/**
 * 
 * @param {String} visibility 
 */
export function filter(todos, visibility = 'all') {
    console.log('filter', todos)
    if (visibility === 'all') {
        return todos
    }

    if (visibility === 'active') {
        return todos.filter(it => !it.completed)
    }

    if (visibility === 'completed') {
        return todos.filter(it => it.completed)
    }

    throw new Error('visibility err :' + visibility)
}